﻿Imports System.Text.RegularExpressions

Public Class Class1
    Public Sub LoadStores(DataGridView1 As DataGridView, Optional ByVal what As String = "Null")
        DataGridView1.Refresh()
        DataGridView1.DataSource = Nothing
        Dim conn As New SqlClient.SqlConnection
        With conn
            .ConnectionString = My.Settings.DataSource
            .Open()
        End With

        Dim cmd As New SqlClient.SqlCommand
        With cmd
            If what = "Null" Then
                .CommandText = "SELECT [p_key],[company],[storename],[type_],[serverip],[mws],[pos1],[pos2],[pos3],[biometrics],[storenumber],[gateway],[pos3],[provider],[status] FROM [Management_Info_Sys].[dbo].[stores] where [gateway] <> ''"
            Else
                .CommandText = "SELECT [p_key],[company],[storename],[type_],[serverip],[mws],[pos1],[pos2],[pos3],[biometrics],[storenumber],[gateway],[pos3],[provider],[status] FROM [Management_Info_Sys].[dbo].[stores] where storename like '%" & what & "%' and [gateway] <> '' ;"
            End If

            .CommandType = CommandType.Text
            .CommandTimeout = 60
            .Connection = conn
        End With
        Dim dt As New DataTable
        dt.Load(cmd.ExecuteReader)

        With DataGridView1
            .AutoGenerateColumns = True
            .DataSource = dt
        End With

        For Each item As DataGridViewTextBoxColumn In DataGridView1.Columns
            'MsgBox(item.HeaderText)

            If item.HeaderText = "company" Then
                item.HeaderText = "Company"
            ElseIf item.HeaderText = "storename" Then
                item.HeaderText = "Store"
            ElseIf item.HeaderText = "type_" Then
                item.HeaderText = "Type"
            ElseIf item.HeaderText = "serverip" Then
                item.HeaderText = "Server IP"
            ElseIf item.HeaderText = "mws" Then
                item.HeaderText = "MWS"
            ElseIf item.HeaderText = "pos1" Then
                item.HeaderText = "POS 1"
            ElseIf item.HeaderText = "pos2" Then
                item.HeaderText = "POS 2"
            ElseIf item.HeaderText = "pos3" Then
                item.HeaderText = "POS 3"
            ElseIf item.HeaderText = "biometrics" Then
                item.HeaderText = "Biometrics"
            ElseIf item.HeaderText = "provider" Then
                item.HeaderText = "Provider"
            ElseIf item.HeaderText = "status" Then
                item.HeaderText = "Status"
            Else
                item.Visible = False
            End If

        Next

        cmd.Dispose()
        cmd = Nothing
        conn.Dispose()
        conn = Nothing
        DataGridView1.Sort(DataGridView1.Columns("company"), System.ComponentModel.ListSortDirection.Ascending)

        'Dim t As New Threading.Thread(AddressOf ping)
        't.Start()
    End Sub

    Function GetMiddleString(Start As String, Ending As String, InputString As String)
        Dim Return_ As String = "Error"
        Try
            Dim Split1 As String() = Regex.Split(InputString, Start, RegexOptions.None)
            Dim Split2 As String() = Regex.Split(Split1(1), Ending, RegexOptions.None)
            End
            Return_ = Split2(0)
        Catch ex As Exception

        End Try
        Return Return_
    End Function
End Class
